Objective c 块目标c内的访问属性
我无法访问此块中的任何属性。就像我在.h文件中创建了UIImage属性,但我无法在该结果块中访问该属性。下一个解决方案是,您可以从整个控制器创建块属性,如下所示:Objective c 块目标c内的访问属性,objective-c,properties,block,Objective C,Properties,Block,我无法访问此块中的任何属性。就像我在.h文件中创建了UIImage属性,但我无法在该结果块中访问该属性。下一个解决方案是,您可以从整个控制器创建块属性,如下所示: - (IBAction)saveBtn:(id)sender { UIImage* imageToSave = [self imageByCombiningImage:self.backgroundImage.image withImage:self.tempImage.image]; ALAssetsLibrary
- (IBAction)saveBtn:(id)sender {
UIImage* imageToSave = [self imageByCombiningImage:self.backgroundImage.image withImage:self.tempImage.image];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Request to save the image to camera roll
[library writeImageToSavedPhotosAlbum:[imageToSave CGImage] orientation:(ALAssetOrientation)[imageToSave imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
NSLog(@"error");
} else {
CGFloat compression = 0.0;
NSData *imageData = UIImageJPEGRepresentation(imageToSave, compression);
UIImage *compressedImage = [[UIImage alloc]initWithData:imageData];
NSMutableString *imageName = [[NSMutableString alloc] initWithCapacity:0];
CFUUIDRef theUUID = CFUUIDCreate(kCFAllocatorDefault);
if (theUUID) {
[imageName appendString:CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, theUUID))];
CFRelease(theUUID);
}
[imageName appendString:@".png"];
NSLog(@"Image name: %@", imageName);
//Image Data to web service
[self uploadImage:UIImageJPEGRepresentation(compressedImage, 1.0) filename:imageName];
_savedImageURL = assetURL;
[library assetForURL:_savedImageURL
resultBlock:resultblock
failureBlock:failureblock];
}];
}
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref) {
UIImage *largeimage = [UIImage imageWithCGImage:iref];
//image Property needs to be access here
}
};
在块内部,您可以使用此属性访问:
__block MyViewController *blocksafeSelf = self;
或调用方法:
blocksafeSelf.myProperty;
如果您发布的代码确实是您正在使用的代码,那么它将定义一个引用块的变量。但是该变量不是类的一部分,因此不能引用任何实例变量 相反,该变量位于“C顶层”,不在任何类中。您可能希望将其转换为实例变量,例如:
dispatch_async(dispatch_get_main_queue(), ^
[blocksafeSelf processNewMessage:text]
});
您也可以使用属性来代替。需要更多信息。这个区块是在哪里定义的?你有什么错误?您的注释提示您试图访问实例变量(可能由属性生成),这才是您真正想要的。请编辑您的问题并提供更多代码和详细信息。请勿在评论中发布代码,谢谢。您在哪里分配块?您发布的代码无效。请修复它。您如何知道无法访问该街区中的某个属性?我们希望您清理代码,请格式化,并查看结果。您的意思是“无法访问”读取或写入,还是两者兼而有之?
\u block
仅用于变量将被分配到的情况<代码>self未分配到此处的任何位置。但您也未在此处使用self
或任何实例变量。如果您这样做了,它将创建一个保留循环。@newacct:您知道在块之前创建对self
的弱引用并在块内部使其变强以打破保留循环的诀窍吗?
@interface MyClass : SomeSuperClass
{
// Define an instance variable.
ALAssetsLibraryAssetForURLResultBlock resultblock;
}
@end
@implementation MyClass
- (void)someMethod
{
// Initialize the variable in `init`, `viewDidLoad` or whichever
// method suits you.
resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref) {
UIImage *largeimage = [UIImage imageWithCGImage:iref];
//image Property needs to be access here
}
};
}
@end