NSThread共享对象问题

发布于 2024-08-26 22:30:41 字数 1729 浏览 8 评论 0原文

我收到“EXC_BAD_ACCESS”,但无法弄清楚原因,任何帮助都会很大 - 提前谢谢您。

用户拍了一张照片,我想在另一个线程中对其进行一些处理...

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

...

NSString *uid = [Asset stringWithUUID];

[_imageQueue setObject:img forKey:uid];

[NSThread detachNewThreadSelector:@selector(createAssetAsyncWithImage:) 
                toTarget:self 
              withObject:uid];
}

然后新线程

-(void) createAssetAsyncWithImage:(NSString *)uid {

 NSAutoreleasePool * pool =[[NSAutoreleasePool alloc] init];

 if ([_imageQueue objectForKey:uid]) {
 Asset *asset = [Asset createAssetWithImage:[_imageQueue objectForKey:uid]];

 asset.uid = uid;

 [self performSelectorOnMainThread:@selector(asyncAssetCreated:)
         withObject:asset 
      waitUntilDone:YES];

 }

 [pool release];
}

调用

    +(Asset *)createAssetWithImage:(UIImage *)img
    { 
   ....

     UIImage *masterImg = [GraphicSupport createThumbnailFromImage:img pixels:img.size.height/2];
    ....

     return newAsset;
    }

然后这就是我不断收到 BAD_ACCESS 的地方

+(UIImage *)createThumbnailFromImage:(UIImage *)inImage pixels:(float)pixels{

 CGSize size = image.size;
 CGFloat ratio = 0;

 if (size.width > size.height) {

  ratio = pixels / size.width;
 }
 else {
  ratio = pixels / size.height;
 }

 CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);

 UIGraphicsBeginImageContext(rect.size);

 //NSAssert(image,@"image NULL");

 [image drawInRect:rect];

 return UIGraphicsGetImageFromCurrentImageContext(); 
}

这是给我所有抱怨的图像...

我是什么我做错了??

预先非常感谢

I'm getting an “EXC_BAD_ACCESS” but just can't work out why, any help would be massive - thank you in advance.

The user takes a picture and I want to do some processing on it in another thread...

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

...

NSString *uid = [Asset stringWithUUID];

[_imageQueue setObject:img forKey:uid];

[NSThread detachNewThreadSelector:@selector(createAssetAsyncWithImage:) 
                toTarget:self 
              withObject:uid];
}

Then the new thread

-(void) createAssetAsyncWithImage:(NSString *)uid {

 NSAutoreleasePool * pool =[[NSAutoreleasePool alloc] init];

 if ([_imageQueue objectForKey:uid]) {
 Asset *asset = [Asset createAssetWithImage:[_imageQueue objectForKey:uid]];

 asset.uid = uid;

 [self performSelectorOnMainThread:@selector(asyncAssetCreated:)
         withObject:asset 
      waitUntilDone:YES];

 }

 [pool release];
}

Which calls

    +(Asset *)createAssetWithImage:(UIImage *)img
    { 
   ....

     UIImage *masterImg = [GraphicSupport createThumbnailFromImage:img pixels:img.size.height/2];
    ....

     return newAsset;
    }

And then this is where I keep getting the BAD_ACCESS

+(UIImage *)createThumbnailFromImage:(UIImage *)inImage pixels:(float)pixels{

 CGSize size = image.size;
 CGFloat ratio = 0;

 if (size.width > size.height) {

  ratio = pixels / size.width;
 }
 else {
  ratio = pixels / size.height;
 }

 CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);

 UIGraphicsBeginImageContext(rect.size);

 //NSAssert(image,@"image NULL");

 [image drawInRect:rect];

 return UIGraphicsGetImageFromCurrentImageContext(); 
}

It's image that is giving me all the complaints...

What am I doing wrong??

Many thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

败给现实 2024-09-02 22:30:41

我发现 UIGraphicsBeginImageContext 不是多线程的,不得不求助于 UIImage 的 Trevors fab 扩展。

http://vocaro.com /trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

I discovered that UIGraphicsBeginImageContext is not multi-threadable and had to resort to Trevors fab extensions to UIImage.

http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文